<?php
/*require_once 'smtpvalidateclassphp.php';
$check_mail = new SMTP_validateEmail();
$check_mail->SMTP_validateEmail(array('chungnd_k48@yahoo.com'));
$check_mail->validate(array('chungnd_k48@yahoo.com'));
*/
class ValidateEmail {
	
	public $check_mail = false;
	
	function ValidateEmail(){
	}
	
	function validate($email){
		$mailparts=explode("@",$email);
		$hostname = $mailparts[1];
		
		// validate email address syntax
		$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
		$b_valid_syntax=eregi($exp, $email);
		
		// get mx addresses by getmxrr
		$b_mx_avail=getmxrr( $hostname, $mx_records, $mx_weight, $other);
		$b_server_found=0;
		
		if($other)
		{
			$host = $mx_records['host'];
			$arr_host = explode(".", $host);
			$host = str_replace($arr_host[0],"mail",$host);
			
			$fp = @fsockopen($host,25, $errno, $errstr, 2);
			if($fp){
				if($this->check_mail)
				{
				  if($this->check_RCPT($fp,$email))
					$b_server_found = 1;
				}
				else
					$b_server_found = 1;		
				 fclose($fp);	
			}
			return $b_server_found;
		}
		
		if($b_valid_syntax && $b_mx_avail){
			 // copy mx records and weight into array $mxs
			 $mxs=array();
			//echo "<pre>".print_r($mx_records,1);die;
			 for($i=0;$i<count($mx_records);$i++){
			   $mxs[$mx_weight[$i]]=$mx_records[$i];
			 }
			//echo "<pre>".print_r($mxs,1);die;
			 // sort array mxs to get servers with highest prio
			 ksort ($mxs, SORT_NUMERIC );
			 reset ($mxs);
		
			 while (list ($mx_weight, $mx_host) = each ($mxs) ) {
			   if($b_server_found == 0){
			
				 //try connection on port 25
				 $fp = fsockopen($mx_host,25, $errno, $errstr, 2);
				 if($fp){
					
					if($this->check_mail)
					{
					  if($this->check_RCPT($fp,$email))
						$b_server_found = 1;
					}
					else
						$b_server_found = 1;
					// quit mail server connection
					
					fclose($fp);
			
				 }
			
			   }
			}
		}
	  return $b_server_found;
	}
	
	function check_RCPT($fp,$email){
		$ms_resp="";
	   // say HELO to mailserver
	   $ms_resp.=$this->send_command($fp, "HELO mail.visky.vn"); //HELO microsoft.com
	
	   // initialize sending mail
	   $ms_resp.=$this->send_command($fp, "MAIL FROM:<vimua@visky.vn>"); //MAIL FROM:<support@microsoft.com>
	
	   // try receipent address, will return 250 when ok..
	   $rcpt_text=$this->send_command($fp, "RCPT TO:<".$email.">");
	   $ms_resp.=$rcpt_text;
	   
	   $ms_resp.=$this->send_command($fp, "QUIT");
	
	   if(substr( $rcpt_text, 0, 3) == "250")
		 return 1;
	   else
		 return 0;  
	}
	
	function send_command($fp, $out){
	
	  fwrite($fp, $out . "\r\n");
	  return $this->get_data($fp);
	}
	
	function get_data($fp){
	  $s="";
	  stream_set_timeout($fp, 2);
	
	  for($i=0;$i<2;$i++)
		$s.=fgets($fp, 1024);
	
	  return $s;
	}

}
// support windows platforms
if (!function_exists ('getmxrr') ) {
	function getmxrr($hostname, &$mxhosts, &$mxweight, &$other) {
		if (!is_array ($mxhosts) ) {
			$mxhosts = array ();
		}
		$other = false;
		if (!empty ($hostname) ) {
			$output = "";
			@exec ("nslookup.exe -type=MX $hostname.", $output);
			$imx=-1;
			
			foreach ($output as $line) {
				$imx++;
				$parts = "";
				//"/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.*)$/" ::: /^\tresponsible mail addr = (.*)$/ responsible mail addr = hostmaster.visky.vn
				if (preg_match ("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.*)$/", $line, $parts) ) {
					$mxweight[$imx] = $parts[1];
					$mxhosts[$imx] 	= $parts[2];
				}
				else if(preg_match ("/^\tresponsible mail addr = (.*)$/", $line, $parts))
				{
					$other 			= true;
					$mxweight[$imx] = 100;
					$mxhosts['host'] 	= $parts[1];
				}
			}
			return ($imx!=-1);
		}
		return false;
	}
}

/* example
$result = new ValidateEmail();
//echo $result->validate('sannhac@admicro.vn').'<br>';
echo $result->validate('chungnd23234@visky123.vn').'<br>';
die;
*/
?>